package com.google.code.swingonfire.component.foldable;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;

public abstract class AbstractJFoldableIndicator extends JComponent implements FoldableListener
{

    private static final long serialVersionUID = 5790978781254438286L;

    private int direction;
    private String actionCommand;

    public AbstractJFoldableIndicator()
    {
        super();
    }

    public int getDirection()
    {
        return direction;
    }

    public void setDirection(int direction)
    {
        if (this.direction != direction)
        {
            this.direction = direction;
        }
    }

    public void addActionListener(ActionListener listener)
    {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener)
    {
        listenerList.add(ActionListener.class, listener);
    }

    public String getActionCommand()
    {
        return actionCommand;
    }

    public void setActionCommand(String actionCommand)
    {
        this.actionCommand = actionCommand;
    }

    protected void fireActionPerformed()
    {
        Object[] listeners = listenerList.getListenerList();
        ActionEvent e = new ActionEvent(this, 0, actionCommand, System.currentTimeMillis(), 0);

        for (int i = listeners.length - 2; i >= 0; i -= 2)
        {
            if (listeners[i] == ActionListener.class)
            {
                ((ActionListener) listeners[i + 1]).actionPerformed(e);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void foldingInProgress(FoldableEvent event)
    {
        repaint();
    }

}
